home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBDELCUR.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  129 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbdelcur.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #ifdef AC_STDLIB
  14. #include <stdlib.h>
  15. #endif
  16. #ifdef AC_STRING
  17. #include <string.h>
  18. #endif
  19.  
  20. /* local headers */
  21. #include "cbase_.h"
  22.  
  23. /*man---------------------------------------------------------------------------
  24. NAME
  25.      cbdelcur - delete current cbase record
  26.  
  27. SYNOPSIS
  28.      #include <cbase.h>
  29.  
  30.      int cbdelcur(cbp)
  31.      cbase_t *cbp;
  32.  
  33. DESCRIPTION
  34.      The cbdelcur function deletes the current record of cbase cbp.
  35.      The record cursor is set to the record following the deleted
  36.      record.  All key cursors are set to null.
  37.  
  38.      cbdelcur will fail if one or more of the following is true:
  39.  
  40.      [EINVAL]       cbp is not a valid cbase pointer.
  41.      [CBELOCK]      cbp is not write locked.
  42.      [CBENOPEN]     cbp is not open.
  43.      [CBENREC]      The record cursor of cbp is null.
  44.  
  45. SEE ALSO
  46.      cbinsert, cbrcursor.
  47.  
  48. DIAGNOSTICS
  49.      Upon successful completion, a value of 0 is returned.  Otherwise,
  50.      a value of -1 is returned, and errno set to indicate the error.
  51.  
  52. ------------------------------------------------------------------------------*/
  53. #ifdef AC_PROTO
  54. int cbdelcur(cbase_t * cbp)
  55. #else
  56. int cbdelcur(cbp)
  57. cbase_t * cbp;
  58. #endif
  59. {
  60.     lspos_t    lspos    = NIL;
  61.     cbrpos_t cbrpos    = NIL;
  62.     void *    buf    = NULL;
  63.     int    i    = 0;
  64.  
  65.     /* validate arguments */
  66.     if (!cb_valid(cbp)) {
  67.         errno = EINVAL;
  68.         return -1;
  69.     }
  70.  
  71.     /* check if not open */
  72.     if (!(cbp->flags & CBOPEN)) {
  73.         errno = CBENOPEN;
  74.         return -1;
  75.     }
  76.  
  77.     /* check if not write locked */
  78.     if (!(cbp->flags & CBWRLCK)) {
  79.         errno = CBELOCK;
  80.         return -1;
  81.     }
  82.  
  83.     /* check if record cursor is null */
  84.     if (lscursor(cbp->lsp) == NULL) {
  85.         errno = CBENREC;
  86.         return -1;
  87.     }
  88.  
  89.     /* get record position */
  90.     if (lsgetcur(cbp->lsp, &lspos) == -1) {
  91.         CBEPRINT;
  92.         return -1;
  93.     }
  94.     cbrpos = lspos;
  95.  
  96.     /* delete keys */
  97.     for (i = 0; i < cbp->fldc; ++i) {
  98.         if (cbp->fldv[i].flags & CB_FKEY) {
  99.             buf = calloc((size_t)1, cbp->fldv[i].len + sizeof(cbrpos_t));
  100.             if (buf == NULL) {
  101.                 CBEPRINT;
  102.                 errno = ENOMEM;
  103.                 return -1;
  104.             }
  105.             if (lsgetrf(cbp->lsp, cbp->fldv[i].offset, buf, cbp->fldv[i].len) == -1) {
  106.                 CBEPRINT;
  107.                 free(buf);
  108.                 return -1;
  109.             }
  110.             memcpy((char *)buf + cbp->fldv[i].len, &cbrpos, sizeof(cbrpos_t));
  111.             if (btdelete(cbp->btpv[i], buf) == -1) {
  112.                 CBEPRINT;
  113.                 free(buf);
  114.                 return -1;
  115.             }
  116.             free(buf);
  117.             buf = NULL;
  118.         }
  119.     }
  120.  
  121.     /* delete current record */
  122.     if (lsdelcur(cbp->lsp) == -1) {
  123.         CBEPRINT;
  124.         return -1;
  125.     }
  126.  
  127.     return 0;
  128. }
  129.